package com.wxmit.ot.gen.enums;

import cn.hutool.core.collection.ListUtil;
import lombok.Getter;
import manifold.rt.api.Array;

import java.util.List;

/**
 * 数据库列 映射 java 字段类型 枚举对象
 *
 * @author huangxiong
 * @version V1.0
 * @date 2023/2/23 14:04
 */
@Getter
public enum Db2JavaFieldEnum  {

    //TODO 这只包导入项
    STRING("String",ListOf("char", "varchar", "nvarchar", "varchar2")),
    TEXT("String",ListOf("tinytext", "text", "mediumtext", "longtext")),

    LONG("Long",ListOf("tinyint", "smallint", "mediumint", "int", "number", "integer",
            "bit", "bigint")),
    BIG_DECIMAL("BigDecimal",ListOf("float", "double", "decimal")),


    DATE("Date",ListOf("datetime", "time", "date", "timestamp"))



    ;

    public final static String DB_MYSQL = "MYSQL";

//    private final String dbType;

    public final String javaFieldType;

    public final List<String> mysqlFieldType;

//    private final String [] oracleFieldType;

    Db2JavaFieldEnum(String javaFieldType,List<String> mysqlFieldType){
        this.javaFieldType = javaFieldType;
        this.mysqlFieldType = mysqlFieldType;
    }

    public static Db2JavaFieldEnum getByDbFieldType(String dbType,String dbFieldType){
        String  lowerCaseDbFieldType = dbFieldType.toLowerCase();
        switch (dbType.toUpperCase()){
            case DB_MYSQL:
                for(Db2JavaFieldEnum item : values()){
                    if(item.mysqlFieldType.anyMatch(lowerCaseDbFieldType::startsWith)){
                        return item;
                    }
                }
                break;
            default:
                throw new IllegalArgumentException(" [ $dbType ] is not a supported type ");
        }
        // 默认 返回 String
        return STRING;
    }

    private static List<String> ListOf(String...args){
        return ListUtil.of(args);
    }
}
